GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 56096b...59eb1b )
by Florian
29s
created

Okapi.icon   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $, google, mytrans, window, Cookies, Coordinates, get_cookie_string
7
*/
8
9
10
var Okapi = {};
11
Okapi.m_map = null;
12
Okapi.m_sites = null;
13
Okapi.m_ready = false;
14
Okapi.m_showCache = null;
15
Okapi.m_enabled = false;
16
Okapi.m_popup = null;
17
Okapi.m_marker = null;
18
Okapi.m_icons = null;
19
Okapi.m_timer = null;
20
21
22
Okapi.init = function (themap) {
23
    'use strict';
24
25
    this.m_map = themap;
26
    var self = this;
27
28
    google.maps.event.addListener(this.m_map, "center_changed", function () {
29
        self.scheduleLoad();
30
    });
31
    google.maps.event.addListener(this.m_map, "zoom_changed", function () {
32
        self.scheduleLoad();
33
    });
34
};
35
36
37
Okapi.parseLocation = function (s) {
38
    'use strict';
39
40
    var loc = s.split("|"),
41
        lat = parseFloat(loc[0]),
42
        lng = parseFloat(loc[1]);
43
44
    if (Coordinates.valid(lat, lng)) {
45
        return new google.maps.LatLng(lat, lng);
46
    }
47
48
    return null;
49
};
50
51
52
Okapi.setShowCache = function (code) {
53
    'use strict';
54
55
    this.m_showCache = code;
56
};
57
58
59
Okapi.setupSites = function () {
60
    'use strict';
61
62
    if (this.m_sites) {
63
        return;
64
    }
65
66
    var self = this,
67
        main_url = "proxy2.php?url=http://www.opencaching.de/okapi/services/apisrv/installations",
68
        keys = {
69
            "Opencaching.DE" : "YSqPufH82encfJ67ZxV2",
70
            "Opencaching.PL" : "jhRyc6rGmT6XEvxva29B",
71
            "Opencaching.NL" : "gcwaesuq3REu8RtCgLDj",
72
            "Opencaching.US" : "GvgyCMvwfH42GqJGL494",
73
            "Opencache.UK" : "7t7VfpkCd4HuxPabfbHd",
74
            "Opencaching.RO" : "gqSWmVJhZGDwc4sRhyy7"
75
        },
76
        prefixes = {
77
            "Opencaching.DE" : "OC",
78
            "Opencaching.PL" : "OP",
79
            "Opencaching.NL" : "OB",
80
            "Opencaching.US" : "OU",
81
            "Opencache.UK" : "OK",
82
            "Opencaching.RO" : "OR"
83
        };
84
85
    this.m_sites = [];
86
87
    $.ajax({
88
        url: main_url,
89
        dataType: 'json',
90
        success: function (response) {
91
            response.map(function (site) {
92
                if (keys[site.site_name] !== undefined) {
93
                    //console.log("adding OC site: " + site.site_name);
94
                    self.m_sites.push({
95
                        siteid: self.m_sites.length,
96
                        name: site.site_name,
97
                        site_url: site.site_url,
98
                        url: site.okapi_base_url,
99
                        proxy: site.okapi_base_url.startsWith('http:'),
100
                        prefix: prefixes[site.site_name],
101
                        key: keys[site.site_name],
102
                        ignore_user: null,
103
                        markers: {},
104
                        finished: true
105
                    });
106
                }
107
            });
108
109
            self.m_ready = true;
110
            if (self.m_enabled) {
111
                self.scheduleLoad(true);
112
            }
113
            if (self.m_showCache && self.m_showCache !== "") {
114
                self.centerMap(self.m_showCache);
115
                self.m_showCache = null;
116
            }
117
        }
118
    });
119
};
120
121
122
Okapi.setupIcons = function () {
123
    'use strict';
124
125
    if (this.m_icons) {
126
        return;
127
    }
128
129
    this.m_icons = {
130
        "Other": new google.maps.MarkerImage("img/cachetype-1.png"),
131
        "Traditional": new google.maps.MarkerImage("img/cachetype-2.png"),
132
        "Multi": new google.maps.MarkerImage("img/cachetype-3.png"),
133
        "Virtual": new google.maps.MarkerImage("img/cachetype-4.png"),
134
        "Webcam": new google.maps.MarkerImage("img/cachetype-5.png"),
135
        "Event": new google.maps.MarkerImage("img/cachetype-6.png"),
136
        "Quiz": new google.maps.MarkerImage("img/cachetype-7.png"),
137
        "Math/Physics": new google.maps.MarkerImage("img/cachetype-8.png"),
138
        "Moving": new google.maps.MarkerImage("img/cachetype-9.png"),
139
        "Drive-In": new google.maps.MarkerImage("img/cachetype-10.png")
140
    };
141
};
142
143
144
Okapi.icon = function (type) {
145
    'use strict';
146
147
    if (this.m_icons[type] !== undefined) {
148
        return this.m_icons[type];
149
    }
150
151
    return this.m_icons.Other;
152
};
153
154
155
Okapi.guessSiteId = function (code) {
156
    'use strict';
157
158
    code = code.toUpperCase();
159
    var siteid;
160
    for (siteid = 0; siteid < this.m_sites.length; siteid += 1) {
161
        if (code.startsWith(this.m_sites[siteid].prefix)) {
162
            return siteid;
163
        }
164
    }
165
166
    return -1;
167
};
168
169
170
Okapi.centerMap = function (code) {
171
    'use strict';
172
173
    if (!this.m_ready) {
174
        //console.log("okapi not ready");
175
        return;
176
    }
177
178
    var siteid = this.guessSiteId(code);
179
    if (siteid < 0) {
180
        //console.log("bad code. cannot determine okapi site");
181
        return;
182
    }
183
184
    this.showPopup(null, code.toUpperCase(), siteid);
185
};
186
187
188
Okapi.createPopupContent = function (code, response) {
189
    'use strict';
190
191
    var content =
192
        '<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />'
193
        + '<table class="cache-popup">'
194
        + '<tr><td>' + mytrans("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>'
195
        + '<tr><td>' + mytrans("geocache.type") + '</td><td>' + response.type + '</td></tr>'
196
        + '<tr><td>' + mytrans("geocache.size") + '</td><td>' + response.size2 + '</td></tr>'
197
        + '<tr><td>' + mytrans("geocache.status") + '</td><td>' + response.status + '</td></tr>'
198
        + '<tr><td>' + mytrans("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>'
199
        + '<tr><td>' + mytrans("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>'
200
        + '<tr><td>' + mytrans("geocache.finds") + '</td><td>' + response.founds + '</td></tr>'
201
        + '</table>';
202
    return content;
203
};
204
205
206
Okapi.showPopup = function (m, code, siteid) {
207
    'use strict';
208
209
    if (!this.m_popup) {
210
        this.m_popup = new google.maps.InfoWindow();
211
    }
212
213
    var self = this,
214
        site = this.m_sites[siteid],
215
        url,
216
        data;
217
218
    url = site.url + 'services/caches/geocache';
219
    data = {
220
        'consumer_key': site.key,
221
        'cache_code': code,
222
        'fields' : 'name|type|status|url|owner|founds|size2|difficulty|terrain|location'
223
    };
224
225
    if (site.proxy) {
226
        data = {
227
            url: url + "?" + $.param(data),
228
        };
229
        url = "proxy2.php";
230
    }
231
232
    $.ajax({
233
        url: url,
234
        dataType: 'json',
235
        data: data,
236
        success: function (response) {
237
            var coords = self.parseLocation(response.location);
238
            self.m_map.setCenter(coords);
239
240
            if (!m) {
241
                m = new google.maps.Marker({
242
                    position: coords,
243
                    map: self.m_map,
244
                    icon: self.icon(response.type)
245
                });
246
                if (self.m_maker) {
247
                    self.m_marker.setMap(null);
248
                }
249
                self.registerPopup(m, code, siteid);
250
                self.m_marker = m;
251
            }
252
253
            self.m_popup.setContent(self.createPopupContent(code, response));
254
            self.m_popup.open(self.m_map, m);
255
        }
256
    });
257
};
258
259
260
Okapi.registerPopup = function (m, code, siteid) {
261
    'use strict';
262
263
    if (!this.m_ready) {
264
        return;
265
    }
266
267
    var self = this;
268
269
    google.maps.event.addListener(m, 'click', function () {
270
        self.showPopup(m, code, siteid);
271
    });
272
};
273
274
275
Okapi.removeMarkers = function () {
276
    'use strict';
277
278
    if (!this.m_ready) {
279
        return;
280
    }
281
282
    this.m_sites.map(function (site) {
283
        var key;
284
        for (key in site.markers) {
285
            if (!site.markers.hasOwnProperty(key)) {
286
                continue;
287
            }
288
            site.markers[key].setMap(null);
289
        }
290
        site.markers = {};
291
    });
292
293
    if (this.m_marker) {
294
        this.m_marker.setMap(null);
295
        delete this.m_marker;
296
        this.m_marker = null;
297
    }
298
};
299
300
301
Okapi.loadBboxSite = function (siteid) {
302
    'use strict';
303
304
    if (!this.m_ready) {
305
        return;
306
    }
307
308
    var self = this,
309
        site = this.m_sites[siteid],
310
        b,
311
        bbox,
312
        url,
313
        data;
314
315
    if (!this.m_enabled) {
316
        site.finished = true;
317
        return;
318
    }
319
320
    if (!site.finished) {
321
        return;
322
    }
323
324
    site.finished = false;
325
326
    b = this.m_map.getBounds();
327
    bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng();
328
329
    url = site.url + 'services/caches/shortcuts/search_and_retrieve';
330
    data = {
331
        'consumer_key': site.key,
332
        'search_method': 'services/caches/search/bbox',
333
        'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}',
334
        'retr_method': 'services/caches/geocaches',
335
        'retr_params': '{"fields": "code|name|location|type|status|url"}',
336
        'wrap': 'false'
337
    };
338
339
    if (site.proxy) {
340
        data = {
341
            url: url + "?" + $.param(data),
342
        };
343
        url = "proxy2.php";
344
    }
345
346
    $.ajax({
347
        url: url,
348
        dataType: 'json',
349
        data: data,
350
        success: function (response) {
351
            var addedCaches = {},
352
                cache,
353
                code;
354
355
            for (code in response) {
356
                if (!response.hasOwnProperty(code)) {
357
                    continue;
358
                }
359
360
                cache = response[code];
361
                if (cache.status !== "Available") {
362
                    continue;
363
                }
364
365
                addedCaches[cache.code] = true;
366
                if (site.markers[cache.code] !== undefined) {
367
                    continue;
368
                }
369
370
                site.markers[cache.code] = new google.maps.Marker({
371
                    position: self.parseLocation(cache.location),
372
                    map: self.m_map,
373
                    icon: self.icon(cache.type)
374
                });
375
376
                self.registerPopup(site.markers[cache.code], cache.code, siteid);
377
            }
378
379
            for (code in site.markers) {
380
                if (site.markers.hasOwnProperty(code) && addedCaches[code] === undefined) {
381
                    site.markers[code].setMap(null);
382
                    delete site.markers[code];
383
                }
384
            }
385
            site.finished = true;
386
        },
387
        error: function () {
388
            //console.log("okapi request failed: " + site.name);
389
            //self.removeMarkersSite(site.markers);
390
            site.markers = {};
391
            site.finished = true;
392
        }
393
    });
394
};
395
396
397
Okapi.loadBbox = function () {
398
    'use strict';
399
400
    if (!this.m_ready) {
401
        return;
402
    }
403
404
    var self = this;
405
    this.m_sites.map(function (site) {
406
        self.loadBboxSite(site.siteid);
407
    });
408
};
409
410
411
Okapi.unscheduleLoad = function () {
412
    'use strict';
413
414
    if (!this.m_ready) {
415
        return;
416
    }
417
418
    if (this.m_timer) {
419
        window.clearTimeout(this.m_timer);
420
        this.m_timer = null;
421
    }
422
};
423
424
425
Okapi.scheduleLoad = function () {
426
    'use strict';
427
428
    if (!this.m_ready) {
429
        return;
430
    }
431
432
    var self = this;
433
434
    this.unscheduleLoad();
435
    this.m_timer = window.setTimeout(function () {
436
        self.loadBbox();
437
    }, 500);
438
};
439
440
441
Okapi.toggle = function (t) {
442
    'use strict';
443
444
    Cookies.set('load_caches', t ? "1" : "0", {expires: 30});
445
    if ($('#geocaches').is(':checked') !== t) {
446
        $('#geocaches').attr('checked', t);
447
    }
448
449
    if (this.m_enabled !== t) {
450
        this.m_enabled = t;
451
    }
452
453
    if (this.m_enabled) {
454
        this.setupIcons();
455
        this.setupSites();
456
        this.scheduleLoad();
457
    } else {
458
        this.unscheduleLoad();
459
        this.removeMarkers();
460
    }
461
};
462
463
464
Okapi.restore = function (defaultValue) {
465
    'use strict';
466
467
    var state = get_cookie_string("load_caches", "invalid");
468
469
    if (state === "0") {
470
        this.toggle(false);
471
    } else if (state === "1") {
472
        this.toggle(true);
473
    } else {
474
        this.toggle(defaultValue);
475
    }
476
};
477